import time
import re

import sys
import io

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')


def parse_time(item):
    try:
        timeArray = time.strptime(item.strip(), "%Y%m%d")
    except:
        try:
            timeArray = time.strptime(item[0:8], "%Y%m%d")
        except:
            try:
                timeArray = time.strptime(item[0:6], "%Y%m")
            except:
                print(item[0:8])
                timeArray = time.strptime("20170101", "%Y%m%d")

    return time.strftime("%Y-%m-%d %H:%M:%S", timeArray)


def parse_status(item):
    if re.search("正在还款", item):
        return 0
    elif re.search("还清", item):
        return 1
    else:
        print(item)


def parse_amount(item):
    unit = 100
    if re.search("万元", item):
        unit = 10000
    if re.search("百万", item):
        unit = 10000 * 100
    if re.search(".", item):
        item = item.split(".")[0]
    try:
        item = float(item)
    except:
        print(item)

    return item * unit


def parse_year_rate(item):
    try:
        item = float(item.split("%")[0])
    except:
        print(item)
    return item


def filter_company(item):
    skip = ["支行", "储蓄所", "合作社", "分行"]
    for i in skip:
        if re.search(i, item) is not None:
            return False
    return True
